//
//  OEPresuppositionViewController.m
//  OneEightyNine
//
//  Created by Mac on 2019/3/5.
//  Copyright © 2019 stoneobs. All rights reserved.
//

#import "OEPresuppositionViewController.h"
#import "OEPresuppShopViewController.h"
@interface OEPresuppositionViewController ()
@property(nonatomic, strong) NSArray                     *peopleArray;/**<  */
@property(nonatomic, strong) NSArray                     *ricepointArray;/**<  */
@property(nonatomic, strong) NSArray                     *fengweiArray;/**<  */
@property(nonatomic, strong) NSArray                     *timeArray;/**<  */

@property(nonatomic, strong) NSMutableArray                     *peopleButtonArray;/**<  */
@property(nonatomic, strong) NSMutableArray                     *ricepointButtonArray;/**<  */
@property(nonatomic, strong) NSMutableArray                     *fengweiButtonArray;/**<  */
@property(nonatomic, strong) NSMutableArray                     *timeButtonArray;/**<  */
@end

@implementation OEPresuppositionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = OESTRING(@"PRESUPPOSITION");
    
    [OEUserDataController sendPresuppositionGroupRequestWithHandle:^(BOOL success, NSArray * _Nonnull fengweiArray, NSArray * _Nonnull ricepointArray, NSArray * _Nonnull peopleArray) {
        self.peopleArray = peopleArray;
        self.ricepointArray = ricepointArray;
        self.fengweiArray = fengweiArray;
        self.timeArray = @[
                           @{@"title":@"11:00-11:30",@"id":@"0"},
                           @{@"title":@"11:30-12:00",@"id":@"0"},
                           @{@"title":@"12:00-12:30",@"id":@"0"},
                           @{@"title":@"12:30-13:00",@"id":@"0"},
                           @{@"title":@"13:00-13:30",@"id":@"0"},
                           @{@"title":@"13:30-14:00",@"id":@"0"},
                           ];
        [self configSubView];
    }];
    
    
    
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    self.tableView.backgroundColor = OE_GrayColor;
    self.view.backgroundColor = OE_GrayColor;
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    CGFloat top = 0;
    CGFloat inset = 10;
    CGFloat buttonHeight = 70;
    CGFloat buttonWith = (UIScreenWidth - inset)/2;
    __weak typeof(self) weakSelf =  self;
    self.peopleButtonArray = NSMutableArray.new;
    self.ricepointButtonArray = NSMutableArray.new;
    self.fengweiButtonArray = NSMutableArray.new;
    self.timeButtonArray = NSMutableArray.new;
    ({
        STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, top, UIScreenWidth, 75)
                                                         text:OESTRING(@"A FEW PEOPLE DINE (1/4)")
                                                    textColor:FirstTextColor
                                                         font:18
                                                  isSizetoFit:NO
                                                textAlignment:NSTextAlignmentLeft];
        [header addSubview:titleLable];
        NSArray * array = self.peopleArray;
        for (NSInteger i = 0; i < array.count; i ++) {
            NSDictionary * dic = array[i];
            NSString *  title = dic[@"title"];
            STButton * button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, buttonWith, buttonHeight)
                                                          title:title
                                                     titleColor:FirstTextColor
                                                      titleFont:15
                                                   cornerRadius:4
                                                backgroundColor:UIColor.whiteColor
                                                backgroundImage:nil
                                                          image:nil];
            button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
            [button setTitleColor:OE_LIMITE_COLOR forState:UIControlStateSelected];
            [button st_setBorderWith:0.5 borderColor:TM_lineColor cornerRadius:2];
            [button setClicAction:^(UIButton *sender) {
                [weakSelf onslectedMoneyButton:sender array:weakSelf.peopleButtonArray];
            }];
            [header addSubview:button];
            [self.peopleButtonArray addObject:button];
            button.left =  inset * (i % 2) + buttonWith  * (i % 2);
            button.top =  titleLable.bottom  +  inset * (i / 2) + buttonHeight  * (i / 2);
            if (i == 0) {
               [weakSelf onslectedMoneyButton:button array:weakSelf.peopleButtonArray];
            }
            top = button.bottom;
        }
    });
    
    ({
        STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, top, UIScreenWidth, 75)
                                                         text:OESTRING(@"RICE POINT (2/4)")
                                                    textColor:FirstTextColor
                                                         font:18
                                                  isSizetoFit:NO
                                                textAlignment:NSTextAlignmentLeft];
        [header addSubview:titleLable];
        NSArray * array = self.ricepointArray;
        for (NSInteger i = 0; i < array.count; i ++) {
            NSDictionary * dic = array[i];
            NSString *  title = dic[@"title"];
            STButton * button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, buttonWith, buttonHeight)
                                                          title:title
                                                     titleColor:FirstTextColor
                                                      titleFont:15
                                                   cornerRadius:4
                                                backgroundColor:UIColor.whiteColor
                                                backgroundImage:nil
                                                          image:nil];
            button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
            [button setTitleColor:OE_LIMITE_COLOR forState:UIControlStateSelected];
            [button st_setBorderWith:0.5 borderColor:TM_lineColor cornerRadius:2];
            [button setClicAction:^(UIButton *sender) {
                [weakSelf onslectedMoneyButton:sender array:weakSelf.ricepointButtonArray];
            }];
            [header addSubview:button];
            [self.ricepointButtonArray addObject:button];
            button.left =  inset * (i % 2) + buttonWith  * (i % 2);
            button.top =  titleLable.bottom  +  inset * (i / 2) + buttonHeight  * (i / 2);
            if (i == 0) {
               [weakSelf onslectedMoneyButton:button array:weakSelf.ricepointButtonArray];
            }
            top = button.bottom;
        }
    });
    
    ({
        STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, top, UIScreenWidth, 75)
                                                         text:OESTRING(@"TIME (3/4)")
                                                    textColor:FirstTextColor
                                                         font:18
                                                  isSizetoFit:NO
                                                textAlignment:NSTextAlignmentLeft];
        [header addSubview:titleLable];
        NSArray * array = self.timeArray;
        for (NSInteger i = 0; i < array.count; i ++) {
            NSDictionary * dic = array[i];
            NSString *  title = dic[@"title"];
            STButton * button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, buttonWith, buttonHeight)
                                                          title:title
                                                     titleColor:FirstTextColor
                                                      titleFont:15
                                                   cornerRadius:4
                                                backgroundColor:UIColor.whiteColor
                                                backgroundImage:nil
                                                          image:nil];
            button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
            [button setTitleColor:OE_LIMITE_COLOR forState:UIControlStateSelected];
            [button st_setBorderWith:0.5 borderColor:TM_lineColor cornerRadius:2];
            [button setClicAction:^(UIButton *sender) {
                [weakSelf onslectedMoneyButton:sender array:weakSelf.timeButtonArray];
            }];
            [header addSubview:button];
            [self.timeButtonArray addObject:button];
            button.left =  inset * (i % 2) + buttonWith  * (i % 2);
            button.top =  titleLable.bottom  +  inset * (i / 2) + buttonHeight  * (i / 2);
            if (i == 0) {
                [self onslectedMoneyButton:button array:weakSelf.timeButtonArray];
            }
            top = button.bottom;
        }
    });
    
    ({
        STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, top, UIScreenWidth, 75)
                                                         text:OESTRING(@"FLAVOR (4/4)")
                                                    textColor:FirstTextColor
                                                         font:18
                                                  isSizetoFit:NO
                                                textAlignment:NSTextAlignmentLeft];
        [header addSubview:titleLable];
        NSArray * array = self.ricepointArray;
        for (NSInteger i = 0; i < array.count; i ++) {
            NSDictionary * dic = array[i];
            NSString *  title = dic[@"title"];
            STButton * button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, buttonWith, buttonHeight)
                                                          title:title
                                                     titleColor:FirstTextColor
                                                      titleFont:15
                                                   cornerRadius:4
                                                backgroundColor:UIColor.whiteColor
                                                backgroundImage:nil
                                                          image:nil];
            button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
            [button setTitleColor:OE_LIMITE_COLOR forState:UIControlStateSelected];
            [button st_setBorderWith:0.5 borderColor:TM_lineColor cornerRadius:2];
            [button setClicAction:^(UIButton *sender) {
                [weakSelf onslectedMoneyButton:sender array:weakSelf.fengweiButtonArray];
            }];
            [header addSubview:button];
            [self.fengweiButtonArray addObject:button];
            button.left =  inset * (i % 2) + buttonWith  * (i % 2);
            button.top =  titleLable.bottom  +  inset * (i / 2) + buttonHeight  * (i / 2);
            if (i == 0) {
                [weakSelf onslectedMoneyButton:button array:weakSelf.fengweiButtonArray];
            }
            top = button.bottom;
        }
    });
    
    header.height = top + 20;
    self.tableView.tableHeaderView = header;
    
    STButton * payButton = [[STButton alloc] initWithFrame:CGRectMake(40, 0, UIScreenWidth - 80, 44)
                                                     title:OESTRING(@"CHOSE A RESTAURANT")
                                                titleColor:UIColor.whiteColor
                                                 titleFont:15
                                              cornerRadius:22
                                           backgroundColor:OE_LIMITE_COLOR
                                           backgroundImage:nil
                                                     image:nil];
    payButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    payButton.top = TMUtils.tabBarTop - 40 ;
    [payButton makeImageRight];
    [self.view addSubview:payButton];
    [payButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedChoseButton];
    }];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsZero;
    self.tableView.frame = CGRectMake(0, TMUtils.navgationBarBootom, UIScreenWidth, payButton.top - TMUtils.navgationBarBootom);

    payButton.top =  payButton.top  +20;
}
#pragma mark --Action Method
- (void)onslectedMoneyButton:(UIButton*)sender array:(NSArray*)array {

    
    for (UIButton * button in array) {
        button.selected = NO;
        button.backgroundColor = UIColor.whiteColor;
    }
    sender.selected = YES;

    UIColor * color = [UIColor colorWithRed:210/255.0 green:255/255.0 blue:154/255.0 alpha:1];
    sender.backgroundColor = color;
}
- (STButton*)buttonWithSelcted:(BOOL)selected{
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"36×36未选中"]];
    buyButton.userInteractionEnabled = NO;
    [buyButton setImage:[UIImage imageNamed:@"34×34选中"] forState:UIControlStateSelected];
    buyButton.selected = selected;
    return buyButton;
}
- (void)onSelctedChoseButton{
    OEPresuppShopViewController * vc = OEPresuppShopViewController.new;
    
    for (NSInteger i = 0; i < self.peopleButtonArray.count; i ++) {
        UIButton * button = self.peopleButtonArray[i];
        if (button.selected) {
            NSDictionary * dic  = self.peopleArray[i];
            NSString * obj_id = [dic[@"id"] description];
            vc.people_id = obj_id;
        }
    }
    
    for (NSInteger i = 0; i < self.ricepointButtonArray.count; i ++) {
        UIButton * button = self.ricepointButtonArray[i];
        if (button.selected) {
            NSDictionary * dic  = self.ricepointArray[i];
            NSString * obj_id = [dic[@"id"] description];
            vc.meal_id = obj_id;
        }
    }
    for (NSInteger i = 0; i < self.fengweiButtonArray.count; i ++) {
        UIButton * button = self.fengweiButtonArray[i];
        if (button.selected) {
            NSDictionary * dic  = self.fengweiArray[i];
            NSString * obj_id = [dic[@"id"] description];
            vc.flavor_id = obj_id;
        }
    }
    
    for (NSInteger i = 0; i < self.timeButtonArray.count; i ++) {
        UIButton * button = self.timeButtonArray[i];
        if (button.selected) {
            NSDictionary * dic  = self.timeArray[i];
            NSString * obj_id = [dic[@"title"] description];
            NSString * strat = [obj_id componentsSeparatedByString:@"-"].firstObject;
            NSString * end = [obj_id componentsSeparatedByString:@"-"].lastObject;
            vc.start_time = [NSString stringWithFormat:@"%@ %@",NSDate.st_localCurrentDate.st_yyyyMMByLineWithDate,strat];
            vc.end_time = [NSString stringWithFormat:@"%@ %@",NSDate.st_localCurrentDate.st_yyyyMMByLineWithDate,end];
        }
    }
    [self.navigationController pushViewController:vc animated:YES];
}
@end
